package com.vegetable.modules.controller.finance;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.PreventDuplicateSubmit;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.FinancialCollectionDetail;
import com.vegetable.modules.entity.finance.po.FinancialCollectionAddPO;
import com.vegetable.modules.entity.finance.po.FinancialCollectionPO;
import com.vegetable.modules.entity.finance.po.FinancialCollectionStatisticsPO;
import com.vegetable.modules.entity.finance.po.ReconciliationPO;
import com.vegetable.modules.entity.finance.vo.FinancialCollectionDetailVO;
import com.vegetable.modules.entity.outbound.SalesOut;
import com.vegetable.modules.entity.outbound.SalesOutDeduction;
import com.vegetable.modules.entity.outbound.po.SalesOutDeductionAddPO;
import com.vegetable.modules.entity.outbound.po.SalesOutDeductionPO;
import com.vegetable.modules.entity.outbound.po.SalesOutDeductionUpdatePO;
import com.vegetable.modules.entity.outbound.po.SalesOutQueryPO;
import com.vegetable.modules.service.finance.IFinancialCollectionDetailService;
import com.vegetable.modules.service.finance.IFinancialCollectionService;
import com.vegetable.modules.service.outbound.ISalesOutDeductionService;
import com.vegetable.modules.service.outbound.ISalesOutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 财务收款管理 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-13
 */
@Slf4j
@RestController
@RequestMapping("/financial/collection")
@Api(value = "销售收款", tags = "销售收款")
public class FinancialCollectionController extends AbstractController {
    @Autowired
    private IFinancialCollectionService financialService;
    @Autowired
    private IFinancialCollectionDetailService financialCollectionDetailService;
    @Autowired
    private ISalesOutService salesOutService;
    @Autowired
    private ISalesOutDeductionService salesOutDeductionService;
    /**
     * 所有销售收款列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询销售收款列表", notes = "条件查询销售收款列表")
    public R list(@RequestParam Map<String, Object> params){
        log.info("条件查询销售收款列表{}", JSON.toJSONString(params));
        PageUtils page = financialService.queryPage(params);
        return R.data( page);
    }

    /**
     * 新增销售收款
     * 20250806 不再手动创建销售收款，而是通过相关流水自动生成
     */
//    @Deprecated
//    @SysLog(value = "新增销售收款")
//    @PostMapping("/saveCollection")
//    @ApiOperation(value = "新增销售收款", notes = "新增销售收款")
//    public R saveCollection(@RequestBody FinancialCollectionAddPO po) throws Exception {
//        log.info("新增销售收款：{}", JSON.toJSONString(po));
//        po.setCreateUserId(getUserId());
//        return financialService.saveCollection(po);
//    }
    @SysLog(value = "分配销售收款")
    @PostMapping("/allocatedCollection")
    @ApiOperation(value = "分配销售收款", notes = "分配销售收款")
    public R allocatedCollection(@RequestBody FinancialCollectionAddPO po) throws Exception {
        log.info("分配销售收款：{}", JSON.toJSONString(po));
        po.setCreateUserId(getUserId());
        return financialService.allocatedCollection(po);
    }
    /**
     * 销售收款详情
     */
    @SysLog(value = "销售收款详情")
    @GetMapping("/detailCollection")
    @ApiOperation(value = "根据id查询销售收款详情", notes = "根据id查询销售收款详情")
    public R detailCollection(Long id){
        if (id == null || id == -1){
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询销售收款详情：{}", JSON.toJSONString(id));
        return financialService.detailCollection(id);
    }

    @SysLog(value = "销售收款详情")
    @GetMapping("/orderCollection")
    @ApiOperation(value = "根据销售出库id查询销售收款详情", notes = "根据销售出库id查询销售收款详情")
    public R orderCollection(Long id){
        if (id == null || id == -1){
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询销售收款详情：{}", JSON.toJSONString(id));
//        SalesOut salesOut = salesOutService.getBaseMapper().selectById(id);
        List<FinancialCollectionDetail> detailList = financialCollectionDetailService.findByOrderId(id);
//        salesOut.setFcDetails(detailList);
        return R.data(detailList);
    }


    /**
     * 查询客商的可对账数据
     */
//    @GetMapping("/reconciliationList")
//    @ApiOperation(value = "查询客商的可对账数据", notes = "查询客商的可对账数据")
//    public R reconciliationList(@RequestParam(value = "customerId") Long customerId){
//        log.info("查询客商的可对账数据{}", customerId);
//        List<FinancialCollectionDetailVO> detailVOList = salesOutService.reconciliationList(customerId);
//        return R.data(detailVOList);
//    }

    @PostMapping("/reconciliationList")
    @ApiOperation(value = "查询客商的可对账数据", notes = "查询客商的可对账数据")
    public R reconciliationList(@RequestBody ReconciliationPO po){
        log.info("销售收款点击对冲查询{}", JSON.toJSONString(po));
        // 调用salesOutService.reconciliationList方法获取对账数据
        if(ObjectUtil.isNull(po.getStartTime())){
            po.setStartTime(new Date());
        }
        if(ObjectUtil.isNull(po.getEndTime())){
            po.setEndTime(new Date());
        }
        List<FinancialCollectionDetailVO> detailVOList = salesOutService.reconciliationList(po);
        return R.data(detailVOList);
    }

    @PostMapping("/salesCollectionStatistics")
    @ApiOperation(value = "条件查询销售收款列表", notes = "条件查询销售收款列表")
    public R salesCollectionStatistics(@RequestBody FinancialCollectionStatisticsPO po){
        log.info("条件查询销售收款列表{}", JSON.toJSONString(po));
        Map<String, Object> page = financialCollectionDetailService.salesCollectionStatistics(po);
        return R.data(page);
    }


    /**
     * 销售出库点击对冲查询可对冲回款明细
     */
    @GetMapping("/hedgedDetailList")
    @ApiOperation(value = "销售出库点击对冲查询可对冲回款明细", notes = "销售出库点击对冲查询可对冲回款明细")
    public R<List<FinancialCollectionDetail>> hedgedDetailList(@RequestParam(value = "id") Long id){
        log.info("销售出库点击对冲查询可对冲回款明细{}", id);
        return financialService.hedgedDetailList(id);
    }

    /**
     * 对冲销售收款确认
     */
    @SysLog(value = "对冲销售收款确认")
    @PostMapping("/submitCollection")
    @PreventDuplicateSubmit(expireTime = 5)
    @ApiOperation(value = "对冲销售收款确认", notes = "对冲销售收款确认")
    public R submitCollection(@RequestBody FinancialCollectionPO po){
        log.info("对冲销售收款确认：{}", JSON.toJSONString(po));
        try {
            return financialService.submitCollection(po);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }


    @SysLog(value = "新增销售扣款")
    @PostMapping("/addDeduction")
    @ApiOperation(value = "新增销售扣款", notes = "新增销售扣款")
    public R addDeduction(@RequestBody List<SalesOutDeductionAddPO> pos){
        log.info("新增销售扣款：{}", JSON.toJSONString(pos));
        for(SalesOutDeductionAddPO po : pos){
            salesOutDeductionService.addDeduction(po);
        }
        return R.success("");
    }

    @SysLog(value = "更新销售扣款")
    @PostMapping("/updateDeduction")
    @ApiOperation(value = "更新销售扣款", notes = "更新销售扣款")
    public R updateDeduction(@RequestBody SalesOutDeductionUpdatePO po){
        log.info("更新销售扣款：{}", JSON.toJSONString(po));
        salesOutDeductionService.updateDeduction(po);
        return R.success("");
    }

    @SysLog(value = "销售扣款列表")
    @PostMapping("/deductionList")
    @ApiOperation(value = "销售扣款列表", notes = "销售扣款列表")
    public R deductionList(@RequestBody SalesOutDeductionPO po){
        log.info("销售扣款列表：{}", JSON.toJSONString(po));
        IPage<SalesOutDeduction> res = salesOutDeductionService.selectPage(po);
        return R.data(res);
    }

    @SysLog(value = "删除回款明细")
    @GetMapping("/deleteDeduction")
    @ApiOperation(value = "删除回款明细", notes = "删除销售扣款")
    public R deleteDeduction(Long id) throws Exception {
        log.info("删除销售扣款：{}", JSON.toJSONString(id));
        Boolean res = financialService.deleteDeduction(id);
        return R.data(res);
    }

    /**
     * 查询未完成回款的销售出库单
     */
    @GetMapping("/uncompletedCollectionList")
    @ApiOperation(value = "查询未完成回款的销售出库单", notes = "查询未完成回款的销售出库单")
    public R uncompletedCollectionList(@RequestParam(value = "customerId") Long customerId){
        log.info("查询未完成回款的销售出库单，customerId:{}", customerId);
        List<FinancialCollectionDetailVO> detailVOList = salesOutService.uncompletedCollectionList(customerId);
        return R.data(detailVOList);
    }

    
}
